<?php declare(strict_types=1);


namespace PinWuYun\domain\service\starLinkServer;

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;

/**
 * 订单类接口
 * Class Orders
 * @package app\starLinkServer
 */
class Order extends Common
{

    /**
     * 预占库存并下单
     * @param array $params
     * @return mixed
     * @throws GuzzleException
     * @throws Exception
     */
    public function preHoleAndAddOrder(array $params): array
    {
        // 预占库存
        $holdData = $params['holdData'];
        $this->preHoldSkuInventory($holdData);
        $response = json_decode($this->preHoldSkuInventory($holdData)->getBody()->getContents(),true);
        if($response['code'] !==  1) {
            throw new Exception('预占库存失败:' . $response['message']);
        }
        // 下单
        return json_decode($this->addOrder($params['orderInfo'])->getBody()->getContents(),true);
    }

    /**
     * 预占商品库存
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function preHoldSkuInventory(array $params): ResponseInterface
    {
        return $this->start('POST', '/goods/preHoldSkuInventory', [
            // 第三方订单号 <= 32位
            'outOrderNo' => $params['outOrderNo'] ?? '',
            // 商品及库存信息json, 参考下面的demo
            'codeInvList' => $params['codeInvList'] ?? '',
            // (限售商品必填)区域ID
            'regionId' => $params['regionId'] ?? '',
        ]);
    }

    /**
     * 释放预占商品库存
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function releaseSkuInventory(array $params): ResponseInterface
    {
        return $this->start('POST', '/goods/releaseSkuInventory', [
            // 第三方订单号 <= 32位
            'outOrderNo' => $params['outOrderNo'] ?? '',
            // 商品及库存信息json, 参考下面的demo
            'codeInvList' => $params['codeInvList'] ?? '',
        ]);
    }

    /**
     * 订单下单
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function addOrder(array $params): ResponseInterface
    {
        return $this->start('POST', '/order/addOrder', [
            // 第三方订单号 <= 32位
            'outOrderNo' => $params['outOrderNo'],
            // 区域ID
            'regionId' => $params['regionId'],
            // 收货人详细地址
            'receiverAddr' => $params['receiverAddr'],
            // 收货人
            'receiver' => $params['receiver'],
            // 收货人电话
            'receiverPhone' => $params['receiverPhone'],
            // 收货人手机号
            'receiverMobile' => $params['receiverMobile'] ?? '',
            // 非必填 不填为 0.00 运费，最长十位 整数8位小数2位
            'freight' => $params['freight'] ?? '',
            // 买家备注
            'buyerRemark' => $params['buyerRemark'] ?? '',
            // 卖家备注
            'sellerRemark' => $params['sellerRemark'] ?? '',
            // 商品列表 json 串，格式参考下面json
            /**
             *  {
                "code": "SL-ECP-6072",  //必填       商品编码
                "quantity": "1"         //必填       数量
                }
             */
            'skuList' => $params['skuList'] ?? '',
            // 用户身份证姓名(订单商品为海淘商品时必填)
            'idcardName' => $params['idcardName'] ?? '',
            //用户身份证号码(订单商品为海淘商品时必填)
            'idcardNo' => $params['idcardNo'] ?? '',
            // 用户身份证正面照地址(订单商品为海淘商品时必填)
            'idcardFirstUrl' => $params['idcardFirstUrl'] ?? '',
            // 用户身份证反面照地址(订单商品为海淘商品时必填)
            'idcardSecondUrl' => $params['idcardSecondUrl'] ?? '',
        ]);
    }

    /**
     * 确认收货
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function confirmOrder(array $params): ResponseInterface
    {
        return $this->start('POST', '/order/confirmOrder', [
            // 星链子订单sn
            'orderSn' => $params['orderSn'] ?? '',
        ]);
    }

    /**
     * 订单详情
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function findOrderByOrderSn(array $params): ResponseInterface
    {
        return $this->start('POST', '/order/findOrderByOrderSn', [
            // 星链子订单sn
            'orderSn' => $params['orderSn'] ?? '',
        ]);
    }

    /**
     * 订单物流信息
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function findExpressInfoByOrderSn(array $params): ResponseInterface
    {
        return $this->start('POST', '/order/findExpressInfoByOrderSn', [
            // 星链子订单sn
            'orderSn' => $params['orderSn'] ?? '',
        ]);
    }

    /**
     * 订单运费查询
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function getOrderFreight(array $params): ResponseInterface
    {
        return $this->start('POST', '/order/getOrderFreight', [
            // 区域ID
            'regionId' => $params['regionId'] ?? '',
            // 收货人详细地址
            'receiverAddr' => $params['receiverAddr'] ?? '',
            // 商品列表 json 串，格式参考下面json
            /**
             * [
                {
                "code": "SL-ECP-6072",  //必填       商品编码
                "quantity": "1"         //必填       数量
                }
            ]
             */
            'skuList' => $params['skuList'] ?? '',
        ]);
    }

    /**
     * 退款申请
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function applyRefund(array $params): ResponseInterface
    {
        return $this->start('POST', '/order/applyRefund', [
            // 星链子订单sn
            'orderSn' => $params['orderSn'],
            // 原因
            'reason' => $params['reason'],
            // 退货上传图片URL集合, json格式 参考下面的demo
            /**
             *  ["","",""]
             */
            'picList' => $params['picList'] ?? '',
        ]);
    }

    /**
     * 退货退款申请
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function applyRefundAndGoods(array $params): ResponseInterface
    {
        return $this->start('POST', '/order/applyRefundAndGoods', [
            // 星链子订单sn
            'orderSn' => $params['orderSn'],
            // 商品编码
            'code' => $params['code'],
            // 原因
            'reason' => $params['reason'],
            // 退货类型，0：退货退款，1：仅退款
            'returnType' => $params['returnType'],
            // 自定义退款金额
            'applyAmount' => $params['applyAmount'],
            // 退货上传图片URL集合, json格式 参考下面的demo
            /**
             *  ["","",""]
             */
            'picList' => $params['picList'] ?? '',
        ]);
    }

    /**
     *  取消退货退款申请
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function cancelApplyRefund(array $params): ResponseInterface
    {
        return $this->start('POST', '/order/cancelApplyRefund', [
            // 星链子订单sn
            'orderSn' => $params['orderSn'],
            // 退款订单编号
            'serviceSn' => $params['serviceSn'],
            // 原因
            'reason' => $params['reason'],
            // 取消退款，2：取消退货退款
            'type' => $params['type'],
        ]);
    }


    /**
     *  查询退货退款状态
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function getReturnOrderStatuts(array $params): ResponseInterface
    {
        return $this->start('POST', '/order/getReturnOrderStatuts', [
            // 退款单编号
            'serviceSn' => $params['serviceSn'] ?? '',
        ]);
    }


    /**
     *  查询物流公司信息
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function getLogisticsCompanyList(): ResponseInterface
    {
        return $this->start('POST', '/order/getLogisticsCompanyList', [
        ]);
    }

    /**
     *  买家发货
     * @param array $params
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function returnOrderGoods(array $params): ResponseInterface
    {
        return $this->start('POST', '/order/returnOrderGoods', [
            // 星链子订单sn
            'orderSn' => $params['orderSn'],
            //退款订单编号
            'serviceSn' => $params['serviceSn'],
            //物流公司编码（物流公司信息ID）
            'deliveryCorpSn' => $params['deliveryCorpSn'],
            // 物流公司名称（物流公司信息名称
            'deliveryCorpName' => $params['deliveryCorpName'],
            // 物流单号
            'deliverySn' => $params['deliverySn'],
        ]);
    }
}